package com.zch.house.service.impl;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zch.house.dao.LeaseContractDao;
import com.zch.house.entity.LeaseContract;
import com.zch.house.service.LeaseContractService;
import com.zch.house.util.XmlSession;

public class LeaseContractServiceImpl extends XmlSession implements LeaseContractService {
	private final int PAGE_SIZE = 20;
	@Override
	public List<LeaseContract> getAllLeaseContractByPage(String number, Date startDate, Date endDate, int state,
			int pageIndex) {
		// 使用map集合来存放参数
		Map<String, Object> map = new HashMap<>();
		map.put("number", number);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("state", state);
		int firstRow = (pageIndex - 1) * PAGE_SIZE + 1;
		int lastRow = pageIndex * PAGE_SIZE;
		map.put("firstRow", firstRow);
		map.put("lastRow", lastRow);
		
		//加载配置文件
		initSession();
		//获得mapper的代理类
		LeaseContractDao dao = session.getMapper(LeaseContractDao.class);
		//调用dao层方法
		List<LeaseContract> list = dao.getAllLeaseContract(map);
		//提交事务并关闭session
		closeSession();
		return list;
	}

	@Override
	public int getTotalCount(String number, Date startDate, Date endDate, int state) {
		// 使用map集合来存放参数
		Map<String, Object> map = new HashMap<>();
		map.put("number", number);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("state", state);
		
		//加载配置文件
		initSession();
		//获得mapper的代理类
		LeaseContractDao dao = session.getMapper(LeaseContractDao.class);
		// 调用dao层方法获得查询到的所有数据的个数
		int counts = dao.getTotalCount(map);
		//提交事务并关闭session
		closeSession();
		// 计算总页数
		int totalPages = counts % PAGE_SIZE == 0 ? counts / PAGE_SIZE : counts / PAGE_SIZE + 1;
		return totalPages;
	}

	@Override
	public LeaseContract getLeaseContractById(int id) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		LeaseContractDao dao = session.getMapper(LeaseContractDao.class);
		// 调用dao层方法
		LeaseContract lc = dao.getLeaseContractById(id);
		//提交事务并关闭session
		closeSession();
		return lc;
	}

	@Override
	public boolean addLeaseContract(LeaseContract lc) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		LeaseContractDao dao = session.getMapper(LeaseContractDao.class);
		// 调用dao层方法
		boolean flag = dao.addLeaseContract(lc);
		//提交事务并关闭session
		closeSession();
		return flag;
	}

	@Override
	public boolean deleteLeaseContract(int id) {
		LeaseContract lc = getLeaseContractById(id);
		lc.setLc_contract_state(0);
		boolean flag = updateLeaseContract(lc);
		if(flag){
			return true;
		}
		return false;
	}

	@Override
	public boolean updateLeaseContract(LeaseContract lc) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		LeaseContractDao dao = session.getMapper(LeaseContractDao.class);
		// 调用dao层方法
		boolean flag = dao.updateLeaseContract(lc);
		//提交事务并关闭session
		closeSession();
		return flag;
	}

}
